package cn.sfa.common.pagination;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.sfa.common.utils.CommonUtil;
/**
 * 
* 类名称：Pagination   
* 类描述： 分页类
* 创建人：lenovo   
* 创建时间：2012-2-9 上午10:26:37    
* 修改备注：   
* @version v1.0
 */
public class Pagination {
	
	Logger log = LoggerFactory.getLogger(Pagination.class);
	public static int NUMBERS_PER_PAGE=10;//每页的记录数,默认为10
	public static PageLabel lable;
	
	private int numPerPage;//每页的记录数
	private int totalPages;// 总页数
	private int totalRows;//总记录数
	private int page;// 当前页码
	private List resultList;//结果集存放List
	private String multiPage;//返回的分页字符串
	
	/**
	 * Pagination构造函数
	 * @param sql  sql语句
	 * @param currentPage 当前页
	 * @param numPerPage 每页记录数
	 * @param url 链接
	 * @param jdbcTemplate 
	 */
	public Pagination(String sql,Map<String,Object> paramMap ,int currentPage, int numPerPage,String url,NamedParameterJdbcTemplate jdbcTemplate) {
		
		if (CommonUtil.isEmpty(jdbcTemplate)) {
			throw new IllegalArgumentException(
					"cn.sfa.common.pagination.Pagination--jdbcTemplate为空,请先进行初始化!");
		} else if (CommonUtil.isEmpty(sql)) {
			throw new IllegalArgumentException(
					"cn.sfa.common.pagination.Pagination--sql语句为空,请先进行初始化!");
		}
		
		
		if(CommonUtil.isEmpty(lable)){
			this.lable= new PageLabel();
		}
		
		if(CommonUtil.isEmpty(numPerPage)){
			setNumPerPage(NUMBERS_PER_PAGE);
		}else{
			setNumPerPage(numPerPage);
		}
		
		if(!url.contains("?")){
			url+="?";
		}else{
			if(!url.endsWith("&")){
				url+="&";
			}
		}
		
		//获取查询总记录数sql
		String countSQL = getSQLCount(sql);
		//设置总页数
		setTotalPages(numPerPage,jdbcTemplate.queryForInt(countSQL,paramMap));
		
		if(currentPage>totalPages){
			currentPage = totalPages;
		}else if(currentPage<1){
			currentPage = 1;
		}
		
		//设置page数
		setPage(currentPage);
				
		int startIndex = (currentPage - 1) * numPerPage;	//数据读取起始index
		
		int rownum = startIndex+numPerPage;
		
		StringBuffer paginationSQL = new StringBuffer(" ");
		
		
//		paginationSQL.append("select * from (select t.*,rownum rn from (");
//		paginationSQL.append(sql);
//		paginationSQL.append(") t ) where rn<=:rownum and rn>:startIndex");
		
		paginationSQL.append(sql);
		
		log.info("paginationSQL=====================:"+paginationSQL.toString());
		
		paramMap.put("rownum", rownum);
		paramMap.put("startIndex", startIndex);
		
		//设置返回的集合
		setResultList(jdbcTemplate.queryForList(paginationSQL.toString(),paramMap));
		
		//设置返回的标签
		getMultiPage(url);
	}
	
	/**
	 * 获取总记录sql
	 * @param sql
	 * @return
	 */
	public String getSQLCount(String sql){
		String sqlBak = sql.toLowerCase();
		String searchValue = " from ";
		String sqlCount = "select count(*) from "+ sql.substring(sqlBak.indexOf(searchValue)+searchValue.length(), sqlBak.length());
		return sqlCount;
	}
	
	/**
	 * 给分页标签字符串赋值
	 * @param url
	 */
	public void getMultiPage(String url){
		
		lable.init(totalRows, numPerPage, totalPages, page, url);
		setMultiPage(lable.getDisplay());
	}
	
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public String getMultiPage() {
		return multiPage;
	}

	public void setMultiPage(String multiPage) {
		this.multiPage = multiPage;
	}
	
	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	// 计算总页数
	public void setTotalPages(int numPerPage,int totalRows) {
		
		setTotalRows(totalRows);
		
		if (totalRows % numPerPage == 0) {
			this.totalPages = totalRows / numPerPage;
		} else {
			this.totalPages = (totalRows / numPerPage) + 1;
		}
	}

}
